﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MSVSTS.Common;
using MSVSTS.DataAccess;
using MSVSTS.Encryption;
using BookOnline.Business;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
namespace BookOnline
{
    public partial class Chapter : AdminMain
    {

        public string returnbody = "";
        public chapters chapter_;
        DMCEncryption Encry = new DMCEncryption();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                AddCss.AddStyleSheet(this.Page, "style\\book1\\css\\menuStyle.css");
                AddCss.AddStyleSheet(this.Page, "style\\book1\\css\\snapStyle.css");

                chapter_ = new chapters();

                if (Request_Class.GetQueryString("chapid").Trim().Length != 0)
                {
                    string chapterid_ = Encry.Decrypt(Encry.DecodeBase64(Request_Class.GetQueryString("chapid").Trim()));
                    if (IsNumeric(chapterid_))
                    {
                        chapter_.ChapterID = int.Parse(chapterid_);
                    }
                    else
                    {
                        Alert("传递参数错误！", "Default.aspx");
                    }
                }
                else
                {
                    chapter_.ChapterID = 1;

                }

                getView();
                getChapterList();
                setTitle(chapter_.ChapterName.ToString());
            }

        }
        private void setTitle(string str)
        {
            if (str.Length < 16)
            {
                this.tools_title_Lab.CssClass = "content_left_tools_size";

            }
            else
            {
                this.tools_title_Lab.CssClass = "content_left_tools_size_";

            }
            this.tools_title_Lab.Text = str;

        }

        public void getChapterList()
        {

           // List<string> list = new List<string>();
            string volumeID = "0";
            StringBuilder sb = new StringBuilder();
            Book_ChapterSys sys = new Book_ChapterSys();
            DataSet ds=new DataSet ();
            try
            {
                string where = " where BookID="+chapter_.BookID.ToString()+" order by orders";
                ds = sys.View_BookChapterDs(where);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (!volumeID.Trim().Equals(dr["VolumeID"].ToString()))
                    {
                        volumeID = dr["VolumeID"].ToString();
                        sb.Append("<tr><td colspan=\"2\"  style=\"background-color:#5699BD;\" >>>" + dr["VolumeName"].ToString() + "</td></tr>");
                    }
                    sb.Append("<tr><td style=\"width:16px;\"><img src=\"style/book1/images/i_rate.jpg\" width=\"16\" height=\"18\" alt=\"\"/></td><td><a href=\"Chapter.aspx?chapid=" + Encry.EncodeBase64(Encry.Encrypt(dr["ID"].ToString())) + "\" class=\"Links\">" + dr["ChapterName"].ToString() + "</a></td></tr>");

                }
                this.Label1.Text = sb.ToString();
            }
            catch (Exception ex)
            { }
            finally
            {
                ds.Dispose();
                ds = null;
                sys = null;
            }

 

        }

        public string getView()
        {

            Book_ChapterSys sys = new Book_ChapterSys();
            DataSet ds=new DataSet ();
            try
            {
                string where = " where id=" + chapter_.ChapterID.ToString() ;
                ds = sys.GetBook_ChapterDs(where);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (IsNumeric(dr["BookID"].ToString()))
                    {
                        chapter_.BookID = int.Parse(dr["BookID"].ToString());
                    }
                    chapter_.ChapterName = dr["ChapterName"].ToString();
                    chapter_.ChapterList .Add( dr["ChapterBody"].ToString());
                    chapter_.ChapterSize =Utils.GetStringLength(dr["ChapterBody"].ToString())/2;
                    break;
                }

                formatBody(chapter_.ChapterList[0].ToString());

                int i_ = 0;
                for (int i = 0; i < chapter_.ChapterList.Count; i++)
                {
                    if (i_ == 0)
                    {
                        returnbody = "\"" + chapter_.ChapterList[i].ToString() + "<br />\"";
                    }
                    else
                        returnbody = returnbody + ",\"" + chapter_.ChapterList[i].ToString() + "<br />\"";
                    i_++;
                }



            }
            catch (Exception ex)
            { }
            finally
            {
                ds.Dispose();
                ds = null;
                sys = null;
            }
            return "";

        
        }

        /// <summary>
        /// 分析章节字符串文字
        /// </summary>
        /// <param name="body"></param>
        public void formatBody(string body)
        {
           
            string Temp = body;
            chapter_.ChapterList.Clear();

            Temp = Utils.ClearSpace(Temp);
            Temp = Utils.ClearBR(Temp);
            Temp = Utils.ReplaceEx(Temp,",","，");
            Temp = Utils.ReplaceEx(Temp,"<p>",",");

            

            String[] arr = Temp.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                string h = arr[i].ToString();
                int start = 0;
                if (h.Trim().Length == 0)
                {
                    continue;
                }
                //int dd = Utils.GetStringLength(h);
                if (Utils.GetStringLength(h)/2 <= chapter_.ChapterH)
                {
                    chapter_.ChapterList.Add("&nbsp;&nbsp;&nbsp;&nbsp;" + h);
                    //start = chapter_.ChapterH;
                    //h = h.Replace("","");
                    continue;
                }
                else
                {
                    int n_ = 0;
                    while (Utils.GetStringLength(h) > chapter_.ChapterH)
                    {
                        if (n_ == 0)
                        {
                            chapter_.ChapterList.Add("&nbsp;&nbsp;&nbsp;&nbsp;" + Utils.CutString(h, 0, chapter_.ChapterH - 2));
                            //start = chapter_.ChapterH;
                            h = Utils.CutString(h, chapter_.ChapterH - 2);
                            n_++;
                        }
                        else
                        {
                            chapter_.ChapterList.Add(Utils.CutString(h, 0, chapter_.ChapterH));
                            //start = chapter_.ChapterH;
                            h = Utils.CutString(h, chapter_.ChapterH);
                        }
                        if (Utils.GetStringLength(h) <= chapter_.ChapterH && h.ToString().Trim().Length>0)
                        {
                            chapter_.ChapterList.Add(h);
                            //start = chapter_.ChapterH;
                            h = Utils.CutString(h, chapter_.ChapterH);
                            break;
                        }
                    }

                }


            }

            chapter_.ChapterCount = chapter_.ChapterList.Count;
            if ((chapter_.ChapterCount % chapter_.ChapterPageNum)>0)
                chapter_.ChapterPage = chapter_.ChapterCount / chapter_.ChapterPageNum+1;
            else
                chapter_.ChapterPage = chapter_.ChapterCount / chapter_.ChapterPageNum;


           // Temp=RegExp.
            //Temp=Utils.CleanInput
            //Temp=Utils.
            //List<string> list = new List<string>();
            

            //return list;
        }
    }

    public class chapters
    {
        //章节编号ID
        public int BookID = 0;

        //章节编号ID
       public int ChapterID = 0;
      
        //字数
       public int ChapterSize = 0;

        //章节名称
       public string ChapterName = "";

        //文章统计行数数组
       public List<string> ChapterList = new List<string>();

        //总行数

       public int ChapterCount = 0;

        //显示行数
       public int ChapterPageNum = 18;

        //页数
       public int ChapterPage = 0;

        //每行显示多少字
       public int ChapterH = 44;
    }
}
